package cn.fangline.camerademo02;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.Size;


/**
 * Created by 抽离 on 2018/11/6.
 */
public class CamerakitActivity extends Activity implements View.OnClickListener{

	private CameraView camera;

	private ImageView photo;

	private boolean mCapturingPicture;

	// To show stuff in the callback
	private Size mCaptureNativeSize;
	private long mCaptureTime;


	private OrientationEventListener mOrientationListener;

	private int rotation;


	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 全屏显示窗口
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.camerakit);

		camera = findViewById(R.id.camera);
		photo = findViewById(R.id.photo);

		camera.addCameraListener(new CameraListener() {
			@Override
			public void onPictureTaken(byte[] picture) {
				onPicture(picture);
			}
		});

		photo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				photo();
			}
		});


		mOrientationListener = new OrientationEventListener(this) {
			@Override
			public void onOrientationChanged(int r) {
				rotation = r;
			}
		};
		mOrientationListener.enable();
	}

	private void photo() {
		if(rotation == -1) {
			message("请将手机抬起！");
			return ;
		} else if(rotation > 5 && rotation < 45) {
			message("左边太高了！");
			return ;
		} else if(rotation >= 45 && rotation < 85) {
			message("右边太高了！");
			return ;
		}  else if(rotation >= 95 && rotation < 135) {
			message("左边太高了！");
			return ;
		} else if(rotation >= 135 && rotation < 175) {
			message("右边太高了！");
			return ;
		} else if(rotation >= 185 && rotation < 225) {
			message("左边太高了！");
			return ;
		} else if(rotation >= 225 && rotation < 265) {
			message("右边太高了！");
			return ;
		} else if(rotation >= 275 && rotation < 315) {
			message("左边太高了！");
			return ;
		} else if(rotation >= 315 && rotation < 355) {
			message("右边太高了！");
			return ;
		}
		camera.capturePicture();
	}

	public void message(String msg) {
		Toast.makeText(CamerakitActivity.this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		camera.start();
	}

	@Override
	protected void onPause() {
		super.onPause();
		camera.stop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		camera.destroy();
	}


	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.photo: capturePhoto(); break;
		}
	}

	private void onPicture(byte[] jpeg) {
		mCapturingPicture = false;
		long callbackTime = System.currentTimeMillis();

		// This can happen if picture was taken with a gesture.
		if (mCaptureTime == 0) mCaptureTime = callbackTime - 300;
		if (mCaptureNativeSize == null) mCaptureNativeSize = camera.getPictureSize();

		PreviewActivity.setImage(jpeg);
		Intent intent = new Intent(CamerakitActivity.this, PreviewActivity.class);
		intent.putExtra("delay", callbackTime - mCaptureTime);
		intent.putExtra("nativeWidth", mCaptureNativeSize.getWidth());
		intent.putExtra("nativeHeight", mCaptureNativeSize.getHeight());
		startActivity(intent);

		mCaptureTime = 0;
		mCaptureNativeSize = null;
	}

	private void capturePhoto() {
		if (mCapturingPicture) return;
		mCapturingPicture = true;
		mCaptureTime = System.currentTimeMillis();
		mCaptureNativeSize = camera.getPictureSize();
		camera.capturePicture();
	}
}
